/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * PlayerSettingGame.java
 *
 * Created on Sep 3, 2011, 3:08:11 PM
 */
package cw.ui;

import com.jtattoo.plaf.aero.AeroLookAndFeel;
import cw.biz.LevelBiz;
import cw.biz.PuzzleSizeBiz;
import cw.biz.SubjectBiz;
import cw.entity.Puzzle;
import cw.entity.PuzzleSize;
import cw.entity.Subject;
import cw.server.ServerConfig;
import cw.server.ServerInterface;
import java.awt.Desktop;
import java.io.File;
import java.io.IOException;
import java.rmi.RemoteException;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;
import javax.swing.UIManager;

/**
 *
 * @author BAOBAO
 */
public class PlayerSettingGame extends javax.swing.JFrame {

    static ServerConfig sc = new ServerConfig();

    /** Creates new form PlayerSettingGame */
    public PlayerSettingGame() {
        initComponents();

    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        txtPlayerName = new javax.swing.JTextField();
        cboSubject = new javax.swing.JComboBox();
        cboGameSize = new javax.swing.JComboBox();
        cboLevel = new javax.swing.JComboBox();
        btnStartGame = new javax.swing.JButton();
        btnHelp = new javax.swing.JButton();
        btnExit = new javax.swing.JButton();
        lblBackground = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("Cross Words - Choose game");
        setResizable(false);

        jPanel1.setLayout(null);

        jLabel1.setFont(new java.awt.Font("Arial", 1, 12));
        jLabel1.setForeground(new java.awt.Color(255, 102, 0));
        jLabel1.setText("Level");
        jPanel1.add(jLabel1);
        jLabel1.setBounds(140, 210, 120, 20);

        jLabel2.setFont(new java.awt.Font("Arial", 1, 12));
        jLabel2.setForeground(new java.awt.Color(255, 102, 0));
        jLabel2.setText("Player Name:");
        jPanel1.add(jLabel2);
        jLabel2.setBounds(140, 90, 120, 20);

        jLabel3.setFont(new java.awt.Font("Arial", 1, 12));
        jLabel3.setForeground(new java.awt.Color(255, 102, 0));
        jLabel3.setText("Game Size:");
        jPanel1.add(jLabel3);
        jLabel3.setBounds(140, 130, 120, 20);

        jLabel4.setFont(new java.awt.Font("Arial", 1, 12));
        jLabel4.setForeground(new java.awt.Color(255, 102, 0));
        jLabel4.setText("Subject:");
        jPanel1.add(jLabel4);
        jLabel4.setBounds(140, 170, 120, 20);
        jPanel1.add(txtPlayerName);
        txtPlayerName.setBounds(270, 90, 150, 20);

        try {
            SubjectBiz subjectBiz=new SubjectBiz();
            List<Subject> lstSubject=new ArrayList<Subject>();
            lstSubject=subjectBiz.getAllSubject();
            for (int i = 0; i < lstSubject.size(); i++) {
                cboSubject.addItem(lstSubject.get(i).getSbjName());
            }
        } catch (Exception e) {}
        cboSubject.addItemListener(new java.awt.event.ItemListener() {
            public void itemStateChanged(java.awt.event.ItemEvent evt) {
                cboSubjectItemStateChanged(evt);
            }
        });
        cboSubject.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cboSubjectActionPerformed(evt);
            }
        });
        jPanel1.add(cboSubject);
        cboSubject.setBounds(270, 170, 150, 20);

        try {
            PuzzleSizeBiz PuzzleSizeBiz=new PuzzleSizeBiz();
            List<PuzzleSize> lstPuzzleSize=new ArrayList<PuzzleSize>();
            lstPuzzleSize=PuzzleSizeBiz.getAllPuzzleSize();
            for (int i = 0; i < lstPuzzleSize.size(); i++) {
                cboGameSize.addItem(lstPuzzleSize.get(i).getSizeName());
            }
        } catch (Exception e) {}
        cboGameSize.addItemListener(new java.awt.event.ItemListener() {
            public void itemStateChanged(java.awt.event.ItemEvent evt) {
                cboGameSizeItemStateChanged(evt);
            }
        });
        cboGameSize.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cboGameSizeActionPerformed(evt);
            }
        });
        jPanel1.add(cboGameSize);
        cboGameSize.setBounds(270, 130, 70, 20);

        try {
            LevelBiz levelBiz=new LevelBiz();
            List<cw.entity.Level> lstLevel=new ArrayList<cw.entity.Level>();
            lstLevel=levelBiz.getAllLevel();
            for (int i = 0; i < lstLevel.size(); i++) {
                cboLevel.addItem(lstLevel.get(i).getLevelName());
            }
        } catch (Exception e) {}
        cboLevel.addItemListener(new java.awt.event.ItemListener() {
            public void itemStateChanged(java.awt.event.ItemEvent evt) {
                cboLevelItemStateChanged(evt);
            }
        });
        jPanel1.add(cboLevel);
        cboLevel.setBounds(270, 210, 70, 20);

        btnStartGame.setIcon(new javax.swing.ImageIcon(getClass().getResource("/images/player_play.png"))); // NOI18N
        btnStartGame.setMnemonic('p');
        btnStartGame.setText(" Play");
        btnStartGame.setOpaque(false);
        btnStartGame.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnStartGameActionPerformed(evt);
            }
        });
        jPanel1.add(btnStartGame);
        btnStartGame.setBounds(270, 260, 100, 30);

        btnHelp.setIcon(new javax.swing.ImageIcon(getClass().getResource("/images/help2.png"))); // NOI18N
        btnHelp.setText("Help");
        btnHelp.setOpaque(false);
        btnHelp.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnHelpActionPerformed(evt);
            }
        });
        jPanel1.add(btnHelp);
        btnHelp.setBounds(400, 260, 81, 30);

        btnExit.setIcon(new javax.swing.ImageIcon(getClass().getResource("/images/back.png"))); // NOI18N
        btnExit.setMnemonic('M');
        btnExit.setText("Main");
        btnExit.setOpaque(false);
        btnExit.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnExitActionPerformed(evt);
            }
        });
        jPanel1.add(btnExit);
        btnExit.setBounds(150, 260, 90, 30);

        lblBackground.setIcon(new javax.swing.ImageIcon(getClass().getResource("/images/settingback.jpg"))); // NOI18N
        jPanel1.add(lblBackground);
        lblBackground.setBounds(0, 0, 600, 330);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, 600, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, 330, Short.MAX_VALUE)
        );

        pack();
        java.awt.Dimension screenSize = java.awt.Toolkit.getDefaultToolkit().getScreenSize();
        java.awt.Dimension dialogSize = getSize();
        setLocation((screenSize.width-dialogSize.width)/2,(screenSize.height-dialogSize.height)/2);
    }// </editor-fold>//GEN-END:initComponents

    private void btnExitActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnExitActionPerformed
        // TODO add your handling code here:
        MainPanel mainPanel = new MainPanel();
        mainPanel.setVisible(true);
        this.dispose();
    }//GEN-LAST:event_btnExitActionPerformed

    private void btnStartGameActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnStartGameActionPerformed
        try {
            // TODO add your handling code here:
            ServerInterface stub = sc.Setup();
            int levelId = stub.getLevelByName(cboLevel.getSelectedItem().toString()).getLevelId();
            int subjectId = stub.getSubjectByName(cboSubject.getSelectedItem().toString()).getSubjectId();
            int sizeId = stub.getSizeByName(cboGameSize.getSelectedItem().toString()).getSizeId();
            String playerName = txtPlayerName.getText().trim();
            Puzzle puzzle = null;
            try {
                puzzle = stub.getPuzzleRandom(levelId, sizeId, subjectId);

                if (puzzle != null) {
                    GameMainPanel gameMainPanel = new GameMainPanel(levelId, subjectId, sizeId, playerName);
                    gameMainPanel.setVisible(true);
                    this.dispose();
                }else{
                    JOptionPane.showMessageDialog(null, "We so sorry! This puzzle is not available!");
                }
            } catch (Exception e) {
//                Logger.getLogger(GameMainPanel.class.getName()).log(Level.SEVERE, null, e);
                JOptionPane.showMessageDialog(null, "We so sorry! This puzzle is not available!");
            }
        } catch (RemoteException ex) {
            Logger.getLogger(PlayerSettingGame.class.getName()).log(Level.SEVERE, null, ex);
        }
    }//GEN-LAST:event_btnStartGameActionPerformed

    private void btnHelpActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnHelpActionPerformed
        try {
            Desktop.getDesktop().open(new File("src/Docs/UserGuide.chm"));
        } catch (IOException ex) {
            JOptionPane.showMessageDialog(null, "User guide is not available");
        }
    }//GEN-LAST:event_btnHelpActionPerformed

    private void cboSubjectActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cboSubjectActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_cboSubjectActionPerformed

    private void cboSubjectItemStateChanged(java.awt.event.ItemEvent evt) {//GEN-FIRST:event_cboSubjectItemStateChanged
        // TODO add your handling code here:
//        JOptionPane.showMessageDialog(null, cboSubject.getSelectedItem().toString());
    }//GEN-LAST:event_cboSubjectItemStateChanged

    private void cboLevelItemStateChanged(java.awt.event.ItemEvent evt) {//GEN-FIRST:event_cboLevelItemStateChanged
        // TODO add your handling code here:
    }//GEN-LAST:event_cboLevelItemStateChanged

    private void cboGameSizeItemStateChanged(java.awt.event.ItemEvent evt) {//GEN-FIRST:event_cboGameSizeItemStateChanged
        // TODO add your handling code here:
    }//GEN-LAST:event_cboGameSizeItemStateChanged

    private void cboGameSizeActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cboGameSizeActionPerformed
    }//GEN-LAST:event_cboGameSizeActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {

                try {
//                    UIManager.setLookAndFeel(new McWinLookAndFeel());
//                    UIManager.setLookAndFeel(new LunaLookAndFeel());
//                    UIManager.setLookAndFeel(new AcrylLookAndFeel());
//                    UIManager.setLookAndFeel(new AluminiumLookAndFeel());
                    UIManager.setLookAndFeel(new AeroLookAndFeel());
                    new PlayerSettingGame().setVisible(true);

                } catch (Exception e) {
                    e.printStackTrace();
                }


            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnExit;
    private javax.swing.JButton btnHelp;
    private javax.swing.JButton btnStartGame;
    private javax.swing.JComboBox cboGameSize;
    private javax.swing.JComboBox cboLevel;
    private javax.swing.JComboBox cboSubject;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JLabel lblBackground;
    private javax.swing.JTextField txtPlayerName;
    // End of variables declaration//GEN-END:variables
}
